package com.example.pongv1;


import java.net.UnknownHostException;

import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;


import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.example.pongv1.SceneManager.SceneType;

public class OptionScene extends BaseScene implements IOnMenuItemClickListener,Constantes{

	private MenuScene optionChildScene;
	private final int optsoundON = 0;
	private final int optsoundOFF = 1;
	private final int level1 = 2;
	private final int level2 = 3;
	private final int level3 = 4;
	private final int level4 = 5;
	private final int level5 = 6;
	private final int level6 = 7;
	private final int level7 = 8;
	private final int level8 = 9;
	private final int level9 = 10;
	private final int level10 = 11;
	private final int bnom = 12;
	private final int bnom_id = 13;
	private final int selectlevel=14;
	private IMenuItem soundonItem;
	private IMenuItem soundoffItem;
	private IMenuItem level1Item;
	private IMenuItem level2Item;
	private IMenuItem level3Item;
	private IMenuItem level4Item;
	private IMenuItem level5Item;
	private IMenuItem level6Item;
	private IMenuItem level7Item;
	private IMenuItem level8Item;
	private IMenuItem level9Item;
	private IMenuItem level10Item;
	private IMenuItem BNOM;
	private IMenuItem selectlevelItem;
	public Text nameText;
	@Override
	public void createScene() {
		createBackground();
		initscene();
	
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
		SceneManager.getInstance().loadMenuScene();
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_OPTION;
		
	}
	
	@Override
	public void disposeScene() {
		RessourcesManager.getInstance().unloadOptionTextures();
		
	}

	
	private void initscene(){
		 optionChildScene = new MenuScene(camera);
		    optionChildScene.setPosition(400, 240);
		    
		    //mise en page des options
		   Text soundText = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight(), ressourcesManager.font, "Sounds:", new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		   Text lineText = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+100, ressourcesManager.font, "----------------------------------------", new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		   lineText.setColor(0.9f,0.5f,0.2f);
		   Text playerText = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+140, ressourcesManager.font, "player name:", new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		   Text lineText1 = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+300, ressourcesManager.font, "----------------------------------------", new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		   lineText1.setColor(0.9f,0.5f,0.2f);
		   Text IAText = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+340, ressourcesManager.font, "IA level:", new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		   
		   nameText = new Text(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+200, ressourcesManager.font3, ressourcesManager.playername, new TextOptions(HorizontalAlign.CENTER), ressourcesManager.vbom);
		  // nameText.setColor(0.9f,0.5f,0.2f);
		   
		   
		   soundonItem = new ScaleMenuItemDecorator(new SpriteMenuItem(optsoundON, ressourcesManager.soundon_region, vbom), 1.2f, 1);
		   soundoffItem = new ScaleMenuItemDecorator(new SpriteMenuItem(optsoundOFF, ressourcesManager.soundoff_region, vbom), 1.2f, 1);
		   level1Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level1, ressourcesManager.level1_region, vbom), 1.2f, 1);
		   level2Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level2, ressourcesManager.level2_region, vbom), 1.2f, 1);
		   level3Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level3, ressourcesManager.level3_region, vbom), 1.2f, 1);
		   level4Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level4, ressourcesManager.level4_region, vbom), 1.2f, 1);
		   level5Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level5, ressourcesManager.level5_region, vbom), 1.2f, 1);
		   level6Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level6, ressourcesManager.level6_region, vbom), 1.2f, 1);
		   level7Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level7, ressourcesManager.level7_region, vbom), 1.2f, 1);
		   level8Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level8, ressourcesManager.level8_region, vbom), 1.2f, 1);
		   level9Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level9, ressourcesManager.level9_region, vbom), 1.2f, 1);
		   level10Item = new ScaleMenuItemDecorator(new SpriteMenuItem(level10, ressourcesManager.level10_region, vbom), 1.2f, 1);
		   selectlevelItem = new ScaleMenuItemDecorator(new SpriteMenuItem(selectlevel, ressourcesManager.selectlevel_region, vbom), 1.2f, 1);
		   BNOM = new ScaleMenuItemDecorator(new SpriteMenuItem(bnom, ressourcesManager.name_region, vbom), 1.2f, 1);

		   optionChildScene.attachChild(soundText);
			optionChildScene.attachChild(playerText);
			optionChildScene.attachChild(lineText);
			optionChildScene.attachChild(lineText1);
			optionChildScene.attachChild(IAText);
			optionChildScene.attachChild(nameText);
			
			
		    optionChildScene.addMenuItem(soundonItem);
		    optionChildScene.addMenuItem(soundoffItem);
		    optionChildScene.addMenuItem(level1Item);
		    optionChildScene.addMenuItem(level2Item);
		    optionChildScene.addMenuItem(level3Item);
		    optionChildScene.addMenuItem(level4Item);
		    optionChildScene.addMenuItem(level5Item);
		    optionChildScene.addMenuItem(level6Item);
		    optionChildScene.addMenuItem(level7Item);
		    optionChildScene.addMenuItem(level8Item);
		    optionChildScene.addMenuItem(level9Item);
		    optionChildScene.addMenuItem(level10Item);
		    optionChildScene.addMenuItem(selectlevelItem);
		    optionChildScene.addMenuItem(BNOM);
		    optionChildScene.buildAnimations();
		    optionChildScene.setBackgroundEnabled(false);
		    
		     soundonItem.setPosition(optionChildScene.getX()-camera.getWidth()+50,optionChildScene.getY()-camera.getHeight()+50);
		     soundoffItem.setPosition(optionChildScene.getX()-camera.getWidth()+400,optionChildScene.getY()-camera.getHeight()+50);
	     level1Item.setPosition(optionChildScene.getX()-camera.getWidth()+80, optionChildScene.getY()-camera.getHeight()+400);
	     level2Item.setPosition(optionChildScene.getX()-camera.getWidth()+150, optionChildScene.getY()-camera.getHeight()+400);
	     level3Item.setPosition(optionChildScene.getX()-camera.getWidth()+220, optionChildScene.getY()-camera.getHeight()+400);
	     level4Item.setPosition(optionChildScene.getX()-camera.getWidth()+290, optionChildScene.getY()-camera.getHeight()+400);
	     level5Item.setPosition(optionChildScene.getX()-camera.getWidth()+360, optionChildScene.getY()-camera.getHeight()+400);
	     level6Item.setPosition(optionChildScene.getX()-camera.getWidth()+430, optionChildScene.getY()-camera.getHeight()+400);
	     level7Item.setPosition(optionChildScene.getX()-camera.getWidth()+500, optionChildScene.getY()-camera.getHeight()+400);
	     level8Item.setPosition(optionChildScene.getX()-camera.getWidth()+570, optionChildScene.getY()-camera.getHeight()+400);
	     level9Item.setPosition(optionChildScene.getX()-camera.getWidth()+640, optionChildScene.getY()-camera.getHeight()+400);
	     level10Item.setPosition(optionChildScene.getX()-camera.getWidth()+710, optionChildScene.getY()-camera.getHeight()+400);
	     
	   BNOM.setPosition(optionChildScene.getX()-camera.getWidth(), optionChildScene.getY()-camera.getHeight()+220);

	     optionChildScene.setOnMenuItemClickListener(this);
	     
	     switch(ressourcesManager.IAlevel){
	       case IALEVEL1:
	     selectlevelItem.setPosition(level1Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	   
	       break;
	       case IALEVEL2:
	  	     selectlevelItem.setPosition(level2Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL3:
	  	     selectlevelItem.setPosition(level3Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL4:
	  	     selectlevelItem.setPosition(level4Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL5:
	  	     selectlevelItem.setPosition(level5Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL6:
	  	     selectlevelItem.setPosition(level6Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL7:
	  	     selectlevelItem.setPosition(level7Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL8:
	  	     selectlevelItem.setPosition(level8Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL9:
	  	     selectlevelItem.setPosition(level9Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       case IALEVEL10:
	  	     selectlevelItem.setPosition(level10Item.getX()+10, optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	       default:
	  	     selectlevelItem.setPosition(level1Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	  	   break;
	     }
		    setChildScene(optionChildScene);
		   
		 
		
		
}
	
	
	private void createBackground()
	{
		attachChild(new Sprite(-CAMERA_WIDTH/2,-CAMERA_HEIGHT/2, ressourcesManager.option_background_region, vbom));
}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		 switch(pMenuItem.getID())
	        {
	        case optsoundON:
	        	ressourcesManager.sound_activated=true;
	        	ressourcesManager.loadMenuAudio();
	        	ressourcesManager.music.play();
	        	//Set in DB the new value of silentmode
	        	   SharedPreferences settings = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor = settings.edit();
	               editor.putBoolean("silentMode", true);

	               // Commit the edits!
	               editor.commit();

	        	
	            return true;
	        case optsoundOFF:
	        	ressourcesManager.sound_activated=false;
	        	ressourcesManager.music.pause();
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings2 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor2 = settings2.edit();
	               editor2.putBoolean("silentMode", false);
	               // Commit the edits!
	               editor2.commit();

	   
	            return true;
	            
	        case level1:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL1;
	        	selectlevelItem.setPosition(level1Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings3 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor3 = settings3.edit();
	               editor3.putInt("IALEVEL", 1);
	               // Commit the edits!
	               editor3.commit();
	        	   return true;
	        	   
	        case level2:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL2;
	        	selectlevelItem.setPosition(level2Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings4 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor4 = settings4.edit();
	               editor4.putInt("IALEVEL", 2);
	               // Commit the edits!
	               editor4.commit();
	        	   return true;
	        case level3:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL3;
	        	selectlevelItem.setPosition(level3Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings5 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor5 = settings5.edit();
	               editor5.putInt("IALEVEL", 3);
	               // Commit the edits!
	               editor5.commit();
	        	   return true;
	        case level4:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL4;
	        	selectlevelItem.setPosition(level4Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings6 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor6 = settings6.edit();
	               editor6.putInt("IALEVEL", 4);
	               // Commit the edits!
	               editor6.commit();
	        	   return true;
	        case level5:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL5;
	        	selectlevelItem.setPosition(level5Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings7 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor7 = settings7.edit();
	               editor7.putInt("IALEVEL", 5);
	               // Commit the edits!
	               editor7.commit();
	        	   return true;
	        case level6:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL6;
	        	selectlevelItem.setPosition(level6Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings8 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor8 = settings8.edit();
	               editor8.putInt("IALEVEL", 6);
	               // Commit the edits!
	               editor8.commit();
	        	return true;
	        case level7:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL7;
	        	selectlevelItem.setPosition(level7Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings9 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor9 = settings9.edit();
	               editor9.putInt("IALEVEL", 7);
	               // Commit the edits!
	               editor9.commit();
	        	   return true;
	        case level8:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL8;
	        	selectlevelItem.setPosition(level8Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings10 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor10 = settings10.edit();
	               editor10.putInt("IALEVEL", 8);
	               // Commit the edits!
	               editor10.commit();
	        	   return true;
	        case level9:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL9;
	        	selectlevelItem.setPosition(level9Item.getX(), optionChildScene.getY()-camera.getHeight()+450);	     
	        	SharedPreferences settings11 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor11 = settings11.edit();
	               editor11.putBoolean("silentMode", false);
	               // Commit the edits!
	               editor11.commit();
	        	   return true;
	        case level10:
	        	ressourcesManager.IAlevel=LEVEL.IALEVEL10;
	        	selectlevelItem.setPosition(level10Item.getX()+10, optionChildScene.getY()-camera.getHeight()+450);	     
	        	//Set in DB the new value of silentmode
	        	 SharedPreferences settings12 = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	               SharedPreferences.Editor editor12 = settings12.edit();
	               editor12.putBoolean("silentMode", false);
	               // Commit the edits!
	               editor12.commit();
	        	   return true;
	        case bnom:
	        	activity.runOnUiThread(new Runnable() {
	        	    @Override
	        	    public void run() {
	        	        activity.showDialog(bnom_id);
	        	        
	        	    }
	        	    
	        	});
	        	
	        	
	        	   return true;
	        default:
	            return false;
	    }
	}
	

	
	
	


}
